package applogic;
import java.util.*;

enum Command{
	CLOSE,
	EXIT,
	OPEN,
	NEW,
	OK,
	CANCEL,
	YES,
	NO,
	LOGIN,
	SIMULATE,
	EDIT,
	SHOW,
	HELP,
	DELETE,
	MESSAGES,
	NEXT,
	PREVIOUS,
	GOTO
	};

public class Terminal
{
	CalenderClient client;
	HashMap<String,Command> commands;
	public Terminal(CalenderClient client)
	{
		this.client = client;
		commands = new HashMap<String,Command>();
		commands.put("close",Command.CLOSE);
		commands.put("exit",Command.EXIT);
		commands.put("open",Command.OPEN);
		commands.put("new",Command.NEW);
		commands.put("ok",Command.OK);
		commands.put("cancel",Command.CANCEL);
		commands.put("yes",Command.YES);
		commands.put("y",Command.YES);
		commands.put("no",Command.NO);
		commands.put("n",Command.NO);
		commands.put("login", Command.LOGIN);
		commands.put("simulate", Command.SIMULATE);
		commands.put("edit", Command.EDIT);
		commands.put("show", Command.SHOW);
		commands.put("help", Command.HELP);
		commands.put("delete", Command.DELETE);
		commands.put("messages", Command.MESSAGES);
		commands.put("next", Command.NEXT);
		commands.put("previous", Command.PREVIOUS);
		commands.put("goto", Command.GOTO);
	}

	public void parseCommand(String input)
	{
		StringTokenizer tokens = new StringTokenizer(input," ");
		if(!tokens.hasMoreElements())
		{
			display("");
			return;
		}
		String commandString = tokens.nextToken().toLowerCase();
		Command command = commands.get(commandString);
		if(command==null){
			display("Invalid command \""+commandString+"\".");
			return;
		}

		String[] arguments = new String[tokens.countTokens()];
		for(int a=0;a<arguments.length;a++)
			arguments[a]=tokens.nextToken();
		try{
			switch(command)
			{
				case CLOSE:
					close();break;
				case EXIT:
					exit();break;
				case OPEN:
					open();break;
				case NEW:
					newc(arguments);break;
				case CANCEL:
					cancel();break;
				case YES:
					yes();break;
				case NO:
					no();break;
				case LOGIN:
					login();break;
				case SIMULATE:
					simulate(input.substring(commandString.length()+1));
					break;
				case EDIT:
					edit(arguments);break;
				case SHOW:
					show(arguments);break;
				case HELP:
					help();break;
				case DELETE:
					delete(arguments);break;
				case MESSAGES:
					client.showMessages();break;
				case NEXT:
					next();break;
				case PREVIOUS:
					previous();break;
				case GOTO:
					goTo(arguments);break;
				default:
					display("Error in program code: Recognized command "
							 +commandString+" does not have associated code");
					break;
			}
		}
		catch(NumberFormatException e)
		{
			display("Error in arguments, type \"help\" to view command properties.\n" +
					"You seem to have put text where a number should have been!");
		}
	}
	
	private void goTo(String[] arguments) {
		client.setWeek(Integer.parseInt(arguments[0]));
	}

	private void previous() {
		client.modWeekMod(-1);
	}

	private void next() {
		client.modWeekMod(1);
	}
	
	private void delete(String[] args)
	{
		client.delete(Integer.parseInt(args[0]));
	}
	
	private void help()
	{
		display("I haven't implemented this yet");
	}
	
	private void show(String[] args)
	{
		if(args==null||args.length==0)
			client.showAppointments(client.user.getUser());
		else
			client.showAppointments(args[0]);
	}
	
	private void edit(String[] args)
	{
		client.edit(Integer.parseInt(args[0]));
	}
	
	private void simulate(String input)
	{
		int target = 0;
		try{
			StringTokenizer st = new StringTokenizer(input," ");
			String targetString = st.nextToken();
			target = Integer.parseInt(targetString);
			input = input.substring(targetString.length()+1);
		}
		catch(NumberFormatException e){
			display("Bad target.");
			return;
		}
		Message msg = new Message(input);
		msg.setTarget(target);
		CalenderClient.connection.sendMessage(msg);
	}

	private void close()
	{
		display("Close command ran!");
	}

	private void exit()
	{
		client.exit();
	}

	private void open()
	{
		display("Open command ran!");
	}

	private void newc(String[] args)
	{
		if(args.length==0 || args[0]==null)
		{
			display("New what?");
		}
		else if(args[0].toLowerCase().equals("meeting"))
		{
			client.createMeeting(0);
		}
		else if(args[0].toLowerCase().equals("appointment"))
		{
			client.createAppointment(0);
		}
		else {
			display("I don't recognize "+args[0]+".");
		}
	}

	private void cancel()
	{
		display("Cancel command ran!");
	}

	private void yes()
	{
		display("Yes command ran!");
	}

	private void no()
	{
		display("No command ran!");
	}
	
	private void login()
	{
		String username = askForInput("Username?");
		String password = askForInput("Password?");
		client.login(username, password);
	}

	public String askForInput(String question)
	{
		Scanner sc = new Scanner(System.in);

		display(question);

		return sc.nextLine();
	}

	public void display(String output)
	{
		System.out.println(output);
		if(client.user!=null)
		{
			System.out.print(client.user);
		}
		System.out.print("->");
	}
}
